<template>
<div>
  <el-dialog
    :title="title"
    size="small"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="单位名称" prop="unitName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.unitName" placeholder="请填写单位名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="正处级领导职数" prop="zhengleaderNumber"
                :rules="[
                 ]">
              <el-input v-model="zhengleaderNumber" placeholder="请填写正处级领导职数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="副处级领导职数" prop="fuleaderNumber"
                :rules="[
                 ]">
              <el-input v-model="fuleaderNumber" placeholder="请填写副处级领导职数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="机构级别" prop="unitLevel"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.unitLevel" placeholder="请填写机构级别"     ></el-input> -->
              <el-select v-model="inputForm.unitLevel" filterable placeholder="请填写性别" style="width:100%;" >
                <el-option v-for="item in unitLevelArr" :label="item.unitLevel" :value="item.unitLevel" :key="item.unitLevel"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="单位编码" prop="unitCode"
                :rules="[
                 ]">
              <el-input v-model="inputForm.unitCode" placeholder="请填写单位编码"     ></el-input>
           </el-form-item>
        </el-col> -->
        <!-- <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                :rules="[
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
    <el-col :span="24">
     <el-form-item label-width="0">
            <el-tabs v-model="basicUnitTab">
            <el-tab-pane label="单位科室">
                  <el-button size="small" @click="addBasicUnitDepartRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.basicUnitDepartList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="departName"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="科室名称">
                  </el-table-column>
                <el-table-column
                    prop="departLeaderNum"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="科级领导职数">
                  </el-table-column>
               <!-- <el-table-column
                    prop="departCode"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="科室编码">
                  </el-table-column> -->
                <el-table-column
                    prop="remarks"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="备注信息">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewBasicUnitDepartRow(scope.row)" type="text" size="small">查看</el-button>
                      <el-button  @click="editBasicUnitDepartRow(scope.row)" type="text" size="small">编辑</el-button>
                      <el-button  @click="delBasicUnitDepartRow(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
           </el-form-item>
          </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
  <BasicUnitDepartForm ref="basicUnitDepartForm" @addRow="saveBasicUnitDepartRow(arguments)"></BasicUnitDepartForm>
</div>
</template>

<script>
  import BasicUnitDepartForm from './BasicUnitDepartForm'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        basicUnitTab: '0',
        zhengleaderNumber: '',
        fuleaderNumber: '',
        unitLevelArr: [],
        inputForm: {
          id: '',
          basicUnitDepartList: [],
          unitName: '',
          leaderNumber: '',
          unitLevel: '',
          unitCode: '',
          sort: '',
          remarks: ''
        }
      }
    },
    components: {
      BasicUnitDepartForm
    },
    methods: {
      init (method, id) {
        this.method = method
        this.unitLevelGetData()
        this.zhengleaderNumber = ''
        this.fuleaderNumber = ''
        if (method === 'add') {
          this.title = `新建基础单位信息`
        } else if (method === 'edit') {
          this.title = '修改基础单位信息'
        } else if (method === 'view') {
          this.title = '查看基础单位信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = id
          this.basicUnitTab = '0'
          this.inputForm.basicUnitDepartList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/basicdata/basicunit/basicUnit/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.basicUnit)
              this.zhengleaderNumber = this.inputForm.leaderNumber !== undefined ? this.inputForm.leaderNumber.split(',')[0] : ''
              this.fuleaderNumber = this.inputForm.leaderNumber !== undefined ? this.inputForm.leaderNumber.split(',')[1] : ''
              this.loading = false
            })
          }
        })
      },
      unitLevelGetData () {
        this.$http({
          url: '/basicdata/basicunitlevel/basicUnitLevel/list',
          method: 'get',
          params: {
            'pageNo': 1,
            'pageSize': 9999
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.unitLevelArr = data.page.list
          }
        })
      },
      saveBasicUnitDepartRow (child) {
        if (child[0] === '') {
          this.inputForm.basicUnitDepartList.push(child[1])
        } else {
          this.inputForm.basicUnitDepartList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.basicUnitDepartList.splice(index, 1, child[1])
            }
          })
        }
      },
      addBasicUnitDepartRow (child) {
        this.$refs.basicUnitDepartForm.init('add')
      },
      viewBasicUnitDepartRow (child) {
        this.$refs.basicUnitDepartForm.init('view', child)
      },
      editBasicUnitDepartRow (child) {
        this.$refs.basicUnitDepartForm.init('edit', child)
      },
      delBasicUnitDepartRow (child) {
        this.inputForm.basicUnitDepartList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.basicUnitDepartList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.basicUnitDepartList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.leaderNumber = this.zhengleaderNumber + ',' + this.fuleaderNumber
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/basicdata/basicunit/basicUnit/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
